package com.frcdi.qms.masterdata.service.impl;

import com.frcdi.qms.common.utils.StringUtils;
import com.frcdi.qms.masterdata.domain.MethodExecute;
import com.frcdi.qms.masterdata.domain.StepData;
import com.frcdi.qms.masterdata.mapper.MethodExecuteMapper;
import com.frcdi.qms.masterdata.mapper.StepDataMapper;
import com.frcdi.qms.masterdata.service.IMethodExecuteService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 方法执行步骤记录Service业务层处理
 *
 * @author cell
 * @date 2023-12-19
 */
@Service
public class MethodExecuteServiceImpl implements IMethodExecuteService
{

    private static final Logger log = LoggerFactory.getLogger(MethodExecuteServiceImpl.class);
    @Autowired
    private MethodExecuteMapper methodExecuteMapper;
    @Autowired
    private StepDataMapper stepDataMapper;

    /**
     * 查询方法执行步骤记录
     *
     * @param executeId 方法执行步骤记录主键
     * @return 方法执行步骤记录
     */
    @Override
    public MethodExecute selectMethodExecuteByExecuteId(Long executeId)
    {
        return methodExecuteMapper.selectMethodExecuteByExecuteId(executeId);
    }

    /**
     * 查询方法执行步骤记录列表
     *
     * @param methodExecute 方法执行步骤记录
     * @return 方法执行步骤记录
     */
    @Override
    public List<MethodExecute> selectMethodExecuteList(MethodExecute methodExecute)
    {
        return methodExecuteMapper.selectMethodExecuteList(methodExecute);
    }

    /**
     * 新增方法执行步骤记录
     *
     * @param methodExecute 方法执行步骤记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertMethodExecute(MethodExecute methodExecute) {
        int rows = methodExecuteMapper.insertMethodExecute(methodExecute);
        // 如果是数值型数据，并判断集合不为空，添加对应数据
        List<StepData> stepDatas = methodExecute.getStepData();
        if (StringUtils.isNotEmpty(stepDatas) && StringUtils.isNotNull(stepDatas)) {
            Long executeId = methodExecute.getExecuteId();
            Long equipment = methodExecute.getEquipment();
            stepDatas.forEach((stepData) -> {
                StepData stepDataEntity = new StepData();
                stepDataEntity.setExecutionId(executeId);
                stepDataEntity.setSerialNumber(stepData.getSerialNumber());
                stepDataEntity.setDataRecord(stepData.getDataRecord());
                stepDataEntity.setEquipment(equipment);

                log.error(stepDataEntity.getExecutionId()+ "");
                log.error(stepDataEntity.getEquipment()+ "");
                stepDataEntity.setRange(stepData.getRange());
                stepDataMapper.insertStepData(stepDataEntity);
            });
        }
        return rows;
    }

    /**
     * 批量删除方法执行步骤记录
     *
     * @param executeIds 需要删除的方法执行步骤记录主键
     * @return 结果
     */
    @Override
    public int deleteMethodExecuteByExecuteIds(Long[] executeIds)
    {
        return methodExecuteMapper.deleteMethodExecuteByExecuteIds(executeIds);
    }
}
